<?php

namespace App\Http\Controllers\Api;

use App\Http\Model\Admin\Out_sourced_weight;
use App\Http\Model\Admin\Planexec;
use App\Http\Model\Admin\SysPublic;
use App\Http\Model\Admin\Out_sourced_weight_detail;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\DB;

use App\Http\Controllers\Controller;

class OutsourcedweightController extends Controller
{
   public function insert()
    {
        $mssage = '{"errorcode":"500","billno":"","success":"false"}';
        if ($input = Input::all()) {
            $bill_no = SysPublic::getautocode('Assistmanu');
            $data['bill_no'] = $bill_no;
            $data['exec_qrcode'] = $input['exec_qrcode'];
            $data['out_sourced_name'] = $input['out_sourced_name'];
            $data['workpiece'] = $input['workpiece'];
            $data['samp_picture'] = $input['samp_picture'];
            $data['samp_piece_count'] = $input['samp_piece_count'];
            $data['samp_piece_weight'] = $input['samp_piece_weight'];
            $data['Unit_samp_weight'] = $input['unit_samp_weight'];
            $data['Unit_samp_set_weight'] = $input['Unit_samp_set_weight'];
            $data['weightgross'] = $input['weightgross'];
            $data['weighttare'] = $input['weighttare'];
            $data['weightnet_plus'] = $input['weightnet_plus'];
            $data['exec_det_id'] = $input['exec_det_id'];
            $data['workpiecenum'] = $input['workpiecenum'];
            $data['modeman'] = $input['modeman'];
            $data['modedate'] = $input['modedate'];
            $data['pstate'] = $input['pstate'];
            $add = Out_sourced_weight::create($data);
            $detail = $input['detail'];
            foreach ($detail as $key => $values) {
                $insertde = DB::insert("insert into t_out_sourced_weight_detail(bill_no,exec_qrcode,print_code,weightbatchs,weightboxcount,
                         Box1TareSet,Box2TareSet,Box3TareSet,Box1TareWeight,Box2TareWeight,Box3TareWeight,weightgross1,
                         weightgross2,weightGross,weightnet,workpiecenumber,modeman,modedate,use_state) values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)",
                    [$bill_no, $detail[$key]['exec_qrcode'], $detail[$key]['print_code'], $detail[$key]['weightbatchs'], $detail[$key]['weightboxcount'], $detail[$key]['Box1TareSet'], $detail[$key]['Box2TareSet'],
                        $detail[$key]['Box3TareSet'], $detail[$key]['Box1TareWeight'], $detail[$key]['Box2TareWeight'], $detail[$key]['Box3TareWeight'], $detail[$key]['weightgross1'], $detail[$key]['weightgross2'], $detail[$key]['weightGross'],
                        $detail[$key]['weightnet'], $detail[$key]['workpiecenumber'], $detail[$key]['modeman'], $detail[$key]['modedate'], '0']);
            }
            if ($add || $insertde) {
                $mssage = '{"errorcode":"200","bill_no":"' . $bill_no . '","success":"true"}';
            } else {
                $mssage = '{"errorcode":"405","bill_no":"","success":"false"}';
            }
        }
        return $mssage;
    }

    public function select()
    {
        $start_time = strtotime(date("Y-m-d"));
        $end_time = strtotime("-3 month", $start_time);
        $db = Out_sourced_weight::where('modedate', '>=', $end_time)->get()->toArray();
        return response()->json($db);
    }

    public function detailselect()
    {
        $input = Input::all();
        $detail = Out_sourced_weight_detail::where('bill_no', $input['bill_no'])->get()->toArray();
        return response()->json($detail);
    }

    public function post_exec_qrcode()
    {
        $input = Input::all();
        $exec = Planexec::where('exec_qrcode', $input['exec_qrcode'])->get()->toArray();
        return response()->json($exec);
    }

    public function update()
    {
        if ($input = Input::all()) {
            $bill_no = $input['bill_no'];
            $data['bill_no'] = $bill_no;
            $data['exec_qrcode'] = $input['exec_qrcode'];
            $data['out_sourced_name'] = $input['out_sourced_name'];
            $data['workpiece'] = $input['workpiece'];
            $data['samp_picture'] = $input['samp_picture'];
            $data['samp_piece_count'] = $input['samp_piece_count'];
            $data['samp_piece_weight'] = $input['samp_piece_weight'];
            $data['Unit_samp_weight'] = $input['unit_samp_weight'];
            $data['Unit_samp_set_weight'] = $input['Unit_samp_set_weight'];
            $data['weightgross'] = $input['weightgross'];
            $data['weighttare'] = $input['weighttare'];
            $data['weightnet_plus'] = $input['weightnet_plus'];
            $data['exec_det_id'] = $input['exec_det_id'];
            $data['workpiecenum'] = $input['workpiecenum'];
            $data['modeman'] = $input['modeman'];
            $data['modedate'] = $input['modedate'];
            $data['pstate'] = $input['pstate'];
            $updb = Out_sourced_weight::where('bill_no', $bill_no)->update($data);
            $deldetail = Out_sourced_weight_detail::where('bill_no', $bill_no)->delete();
            $detail = $input['detail'];
            foreach ($detail as $key => $values) {
                $insertde = DB::insert("insert into t_out_sourced_weight_detail(bill_no,exec_qrcode,print_code,weightbatchs,weightboxcount,
                         Box1TareSet,Box2TareSet,Box3TareSet,Box1TareWeight,Box2TareWeight,Box3TareWeight,weightgross1,
                         weightgross2,weightGross,weightnet,workpiecenumber,modeman,modedate,use_state) values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)",
                    [$bill_no, $detail[$key]['exec_qrcode'], $detail[$key]['print_code'], $detail[$key]['weightbatchs'], $detail[$key]['weightboxcount'], $detail[$key]['Box1TareSet'], $detail[$key]['Box2TareSet'],
                        $detail[$key]['Box3TareSet'], $detail[$key]['Box1TareWeight'], $detail[$key]['Box2TareWeight'], $detail[$key]['Box3TareWeight'], $detail[$key]['weightgross1'], $detail[$key]['weightgross2'], $detail[$key]['weightGross'],
                        $detail[$key]['weightnet'], $detail[$key]['workpiecenumber'], $detail[$key]['modeman'], $detail[$key]['modedate'], '0']);
            }
            if ($updb or $insertde) {
                $mssage = '{"errorcode":"200","success":"true"}';
            } else {
                $mssage = '{"errorcode":"405","success":"false"}';
            }
            return $mssage;
        }
    }

    public function deletedetail() {
        $message = '{"errorcode":"500","bill_no":"",""print_code:""，"success":"false"}';
        if ($input = Input::all()) {
            $deldetail = Out_sourced_weight_detail::where('bill_no',$input['bill_no'])->where('print_code',$input['print_code'])->delete();
            //$deldetail = DB::delete('delete from t_out_sourced_weight_detail where bill_no=? and print_code=?',[$input['bill_no'],$input['print_code']]);
            if ($deldetail) {
                $message = '{"errorcode":"200","success":"true"}';
            } else {
                $message = '{"errorcode":"405","success":"false"}';
            }
        }
        return $message;
    }

    public function updateweightnote()
    {
        $mssage = '{"errorcode":"500","billno":"","success":"false"}';;
        if ($input = Input::all()) {
            $bill_no = $input['bill_no'];
            $data['bill_no'] = $bill_no;
            $data['exec_qrcode'] = $input['exec_qrcode'];
            $data['out_sourced_name'] = $input['out_sourced_name'];
            $data['workpiece'] = $input['workpiece'];
            $data['samp_picture'] = $input['samp_picture'];
            $data['samp_piece_count'] = $input['samp_piece_count'];
            $data['samp_piece_weight'] = $input['samp_piece_weight'];
            $data['Unit_samp_weight'] = $input['unit_samp_weight'];
            $data['Unit_samp_set_weight'] = $input['Unit_samp_set_weight'];
            $data['weightgross'] = $input['weightgross'];
            $data['weighttare'] = $input['weighttare'];
            $data['weightnet_plus'] = $input['weightnet_plus'];
            $data['exec_det_id'] = $input['exec_det_id'];
            $data['workpiecenum'] = $input['workpiecenum'];
            $data['modeman'] = $input['modeman'];
            $data['modedate'] = $input['modedate'];
            $data['pstate'] = $input['pstate'];
            $updb = Out_sourced_weight::where('bill_no', $bill_no)->update($data);
            if ($updb) {
                $mssage = '{"errorcode":"200","success":"true"}';
            } else {
                $mssage = '{"errorcode":"405","success":"false"}';
            }
        }
        return $mssage;
    }

    public function ddde() {
        /*$pdo = DB::connection() -> getPdo();
        dd($pdo);*/
        $db = Out_sourced_weight::get()->toArray();
        return response()->json($db);
        /*$db = DB::table('t_out_sourced_weight_detail')->get()->toArray();
        return response()->json($db);*/
    }
}

